// 优惠券
import {myRequest} from '@/api/ajax.js'
export default {
	namespaced: true,
	state: {
		// 待领取的优惠券
		availableCoupons: []
	},
	
	mutations: {
		add_availableCoupons(state, coupon) {
			state.availableCoupons.push(coupon)
			console.log('待领取的优惠券为：', state.availableCoupons)
		},
		
		// 根据优惠券id删除优惠券
		deleteById(state, couponId) {
			for (let i = 0; i < state.availableCoupons.length; i++) {
				if (couponId == state.availableCoupons[i].yhqID) {
					state.availableCoupons.splice(i, 1);
					break;
				}
			}
		}
	},
	
	actions: {
		// socket收到尤新的优惠券时调用此函数
		async getNewCoupon({state, commit}, data) {
			// console.log('来了铁子')
			let strs = data.msg.split(' ')
			// strs[1]是优惠券id
			// console.log('strs[1] = ', strs[1])
			
			let result = await myRequest({
				url: '/newtest/shop-mjq/getyhq/' + strs[1],
			})
			
			let coupon = result.data.res
			
			// console.log("优惠券具体信息为：", coupon)
			
			commit('add_availableCoupons', coupon)
		}
	},
	
	getters: {
	
	}
}
